﻿using System.Windows.Forms;
using System.Linq;
using IGM.Framework.Constants;

namespace IGM.Framework.Commons
{
    public static class DataGridViewHelper
    {
        /// <summary>
        /// This function will make almost columns of data grid view is read only without 
        /// list field name from parameter
        /// </summary>
        /// <param name="view"></param>
        /// <param name="withoutColumns"></param>
        public static void CreateReadOnlyWithoutColumns(DataGridView view, string[] withoutColumns)
        {
            if (view == null) return;

            for (var i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].ReadOnly = !withoutColumns.Contains(view.Columns[i].Name);
            }
        }

        /// <summary>
        /// This function will help you hide columns without list columns name from parameter
        /// </summary>
        /// <param name="view"></param>
        /// <param name="includeColumns"></param>
        public static void ShowHideColumns(DataGridView view, string[] includeColumns)
        {
            for (var i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].Visible = includeColumns.Contains(view.Columns[i].Name);
            }
        }



        /// <summary>
        /// This function will help you design property isChecked
        /// </summary>
        /// <param name="view"></param>
        public static void ShowIsCheck(DataGridView view)
        {
            if (view == null || view.Columns[PropertiesConstants.IsChecked] == null) return;

            view.Columns[PropertiesConstants.IsChecked].DisplayIndex = 0;

            view.Columns[PropertiesConstants.IsChecked].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

        }    

        /// <summary>
        /// This function will help you check all, or not row in data grid view
        /// </summary>
        /// <param name="view"></param>
        /// <param name="isCheckAll"></param>
        public static void ApplyCheckedAllOrUncheckAll(DataGridView view, bool isCheckAll)
        {
            for (var i = 0; i < view.Columns.Count; i++)
            {
                if (view.Columns[i].Name != PropertiesConstants.IsChecked) continue;

                for (var j = 0; j < view.RowCount; j++)
                {
                    view[i, j].Value = isCheckAll;
                }
            }
        }

        /// <summary>
        /// This function will help you check all row of datagridview from parameter
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public static bool AllRowsIsCheck(DataGridView view)
        {
            for (var i = 0; i < view.RowCount; i++)
            {
                if (!(bool)view[PropertiesConstants.IsChecked, i].Value)
                {
                    return false;
                }
            }
            return true;
        }

        public static void SetDgvCurrentCell(DataGridView view, string name)
        {
            view.ClearSelection();
            for (var i = 0; i < view.RowCount; i++)
            {
                if (!Equals(view.Rows[i].Cells[PropertiesConstants.Name].Value, name)) continue;
                view.Rows[i].Cells[PropertiesConstants.Name].Selected = true;
                break;
            }
        }

        public static void ChangeDgvHeaderText(DataGridView view, string[] oldColumnNames, string[] newColumnNames)
        {
            if (view == null || oldColumnNames.Length != newColumnNames.Length) return;
            for (var i = 0; i < oldColumnNames.Length; i++)
                view.Columns[oldColumnNames[i]].HeaderText = newColumnNames[i];
        }
    }
}
